package Model
{
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ObjectUtil;
	
	[Bindable]
	public class Topics
	{
		public static var _instance:Topics;
		private var _all:ArrayCollection;
		private var _selectedIndices:Array;
		public var _currentIndex:Number;
		
		public function Topics()
		{
			if(_instance != null)
			{
			   throw new Error("Topics cannot be instanciated. You can only use it's properties");
			}
			_instance = this;
		}
		
		public static function get instance():Topics
		{
            if(_instance == null) 
			{
				_instance = new Topics();
			}
			return _instance;
		}
		
		public function get all():ArrayCollection
		{
			return _all;
		}
		
		public function set all(topics:ArrayCollection):void
		{
			_all = new ArrayCollection(topics.source);
		}
		
		[Bindable(event="currentChanged")]
		public function current():Topic
		{
			if(_currentIndex == -1)
				return null;
			return _all[_currentIndex];
		}
		
		
		public function set currentIndex(index:Number):void
		{
			_currentIndex = index;
			var eventObj:Event = new Event("currentChanged");
			dispatchEvent(eventObj);
		}
		
		[Bindable(event="selectedChanged")]
		public function getSelectedTopics():ArrayCollection
		{
			var selectedTopics:ArrayCollection = new ArrayCollection();
			var i:Number;
			for(i = 0; i < _selectedIndices.length; i++)
			{
				var index:Number = _selectedIndices[i];
				selectedTopics.addItem(_all[index]);
			}

			return selectedTopics;
		}
		
		[Bindable(event="selectedChanged")]
		public function get selectedIndices():Array
		{
			return _selectedIndices;
		}
		

		public function set selectedIndices(topicIndices:Array):void
		{
			//_selectedIndexes = topicIndexes.concat();
			_selectedIndices = topicIndices;
			var eventObj:Event = new Event("selectedChanged");
			dispatchEvent(eventObj);
		}
		
		[Bindable(event="selectedChanged")]
		[Bindable(event="currentChanged")]
		public function getSelectedTopicNames():ArrayCollection
		{
			var i:Number;
			var names:ArrayCollection = new ArrayCollection();
			for(i = 0; i < selectedIndices.length; i++)
			{
				names.addItem(all[selectedIndices[i]].TopicName);
			}
			return names;
		}
		
		public function getIndex(id:int):int
		{
			var index:int = -1;
			for(var i:int=0; i<all.length; i++)
				if(all[i].TopicID == id)
				{
					index = i;
					break;
				}
			return index;
		}
		
		public function selectOneTopic(index:int):void
		{
			selectedIndices = [];
			selectedIndices.push(index);
			var eventObj:Event = new Event("selectedChanged");
			dispatchEvent(eventObj);
		}
		
		public function addSelectedTopic(id:int):void
		{
			for(var i:int = 0; i < _selectedIndices.length; i++)
				if(_selectedIndices[i] == id)
					return;
			_selectedIndices.push(id);
		}
		
		public function dateGroups():ArrayCollection
		{
				
			var arrCopy:Array = new Array();
			for (var j:int = 0; j < _all.length; j++) {
				arrCopy.push(_all[j]);
			}
			arrCopy.sortOn(["_lastPostDate"],Array.NUMERIC, Array.DESCENDING);
			var dateTopics:ArrayCollection = new ArrayCollection();
			var i:int = 0;
			var len:int = arrCopy.length;
			while(i < len)
			{
				var currentDate:Date = arrCopy[i].lastPostDate;
				var arr:ArrayCollection = new ArrayCollection();
				arr.addItem(arrCopy[i]);
				var group:DateGroup = new DateGroup(currentDate, arr);
				i++;
				while(i < len && arrCopy[i].lastPostDate.toDateString() == currentDate.toDateString()) 
				{
					group.addTopic(arrCopy[i]);
					i++;
				}
				dateTopics.addItem(group);
			}
			
			return dateTopics;
			
		}
		
		public function getFontWeight(topic:Topic):String
		{
			if(topic.TopicID == current().TopicID)
		      return "bold";
			  else return "normal";
		}
		
		public function getFontColor(topic:Topic):uint
		{
			if(topic.TopicID == current().TopicID)
				return 	7637829;
			else return 0;
		}
		
		public function getFontSize(topic:Topic):int
		{
			if(topic.TopicID == current().TopicID)
				return 	20;
			else return 14;
		}
	}
}